pub(crate) mod sql_const;
pub(crate) mod user_db;

use lazy_static::lazy_static;
use rusqlite::{Connection, Transaction};
use std::cell::OnceCell;
use std::sync::{Arc, OnceLock};
use tokio::sync::{Mutex, RwLock};
static INSTANCE: OnceLock<Mutex<Database>> = OnceLock::new();

pub fn get_db() -> &'static Mutex<Database> {
    INSTANCE.get_or_init(|| Mutex::new(Database::new("")))
}

pub(crate) struct Database {
    pub(crate) conn: Connection,
}

impl Database {
    /* 新建 */
    pub(crate) fn new(db_path: &str) -> Self {
        let conn = Connection::open(db_path).expect("连接数据库失败");
        Database { conn }
    }

    pub(crate) fn build_self() -> Arc<Mutex<Self>> {
        let conn = Connection::open("lipo_test.db").expect("连接数据库失败");
        Arc::new(Mutex::new(Database { conn }))
    }

 

    /* 创建table 失败 */
    pub(crate) fn create_table(&self, sql: &str) {
        self.conn.execute(sql, []).expect("创建table失败。");
    }
}

// lazy_static! {
//     static ref DB: Arc<RwLock<Database>> = {
//         let db_path = "lipo_test.db";
//         let db = Database::new(db_path);
//         // 初始化 table
//         db.create_table(sql_const::USER_CREATE_TAB);
//
//         Arc::new(RwLock::new(db))
//     };
// }